package com.steven.door.socket;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.steven.door.entity.SocketRegisterEntity;


import me.apon.lemon.Lemon;
import me.apon.lemon.core.ConnectHandler;
import me.apon.lemon.core.Disposable;
import me.apon.lemon.core.MessageHandler;
import me.apon.lemon.core.SocketClient;
import me.apon.lemon.protocols.TextProtocols;


public class AndroidSocketClient {
    private static String TAG = "AndroidSocketClient";
    private Context mContext;
    @SuppressLint("StaticFieldLeak")
    private static AndroidSocketClient androidSocketClient;
    private static Lemon lemon;

    private Disposable connect, message;

    private AndroidSocketClient(Context context) {
        this.mContext = context;
        initSocket();//配置本地的socket
    }

    public static AndroidSocketClient getInstance(Context context) {
        if (androidSocketClient == null) {
            synchronized (AndroidSocketClient.class) {
                androidSocketClient = new AndroidSocketClient(context);
            }
        }

        return androidSocketClient;
    }

    //初始化socket客户端
    private void initSocket() {
        SocketClient socketClient = new SocketClient.Builder()
                .ip("122.114.66.228") //设置ip、端口
                .port(8282)
                .setKeepAlive(true) //设置socket选项
                .build();
        //心跳包内容
        String pingData = "{\"type\":\"ping-alive\"}";
        lemon = new Lemon.Builder()
                .client(socketClient) //设置SocketClient
                .protocols(TextProtocols.create()) //设置协议，可自定义
                .pingInterval(10) //设置心跳间隔（秒）大于0打开心跳功能
                .pingData(pingData.getBytes()) //设置心跳包内容
                .debug(true) //开启调试模式
                .build();
    }

    //连接服务
    public void localConnect() {
        lemon.connect();
        connect = lemon.onConnect(new ConnectHandler() {

            @Override
            public void connectSuccess() {
                Log.e(TAG, "===连接成功===");
                registerDevice();
                Toast.makeText(mContext, "连接成功!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void connectFail() {
                Log.e(TAG, "===连接失败===");
            }

            @Override
            public void disconnect() {
                Log.e(TAG, "===连接断开===");
            }

        });
        message = lemon.onMessage(new MessageHandler() {
            @Override
            public void receive(byte[] data) {
                Log.e(TAG, "receive: " + new String(data));
            }
        });

    }

    //断开连接
    public void logout() {
        lemon.disconnect();
        connect.dispose();
        message.dispose();
    }

    //发送消息
    public void sendMessage(String msg) {
        lemon.send("try to have fun!");
    }


    //注册设备
    private void registerDevice() {
        SocketRegisterEntity socketRegisterEntity = new SocketRegisterEntity();
        socketRegisterEntity.setUid("123");
        socketRegisterEntity.setType("1");
        lemon.send(JSON.toJSONString(socketRegisterEntity));
    }

}
